package com.weiwu.weiwustore.controller;

import com.weiwu.weiwustore.pojo.MaterialListVo;
import com.weiwu.weiwustore.pojo.RestResponse;
import com.weiwu.weiwustore.pojo.StoreListVo;
import com.weiwu.weiwustore.service.StoreService;
import com.weiwu.weiwustore.utils.CrudUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author Dreamchser
 */
@RestController
public class StoreController {
    @Autowired
    StoreService storeService;

    @GetMapping("/store/getstoreitem")
    public RestResponse getselectitem() {
        return new RestResponse(storeService.findDepositoryItem());
    }


    /**
     * 获取仓库列表 仓库管理用
     *
     * @param map 分页数据和查询条件
     * @return 出库列表数据
     */
    @PostMapping("/store/getstorelist")
    public RestResponse getstorelist(@RequestBody Map<String, Object> map) {
        List<StoreListVo> res= storeService.getstorelist(map);
        Integer count = resCount(map);
        if (count == 0) {
            count = res.size();
        }
        return new RestResponse(res, count, 200);
    }

    /**
     * 新增材料
     * @param map
     * @return 是否成功
     */
    @PostMapping("/store/storeadd")
    public RestResponse storeadd(@RequestBody Map<String, Object> map) {
        return CrudUtil.postHandle(storeService.storeadd(map), 1);
    }
    /**
     * 编辑材料 保存
     * @param map
     * @return 是否成功
     */
    @PostMapping("/store/storesave")
    public RestResponse storesave(@RequestBody Map<String, Object> map) {
        return CrudUtil.postHandle(storeService.storesave(map), 1);
    }
    /**
     * 返回搜索查询返回的数据总数  如果默认查询全部则返回数据库内总数
     *
     * @param map 查询条件
     * @return 要么返回默认查询全部的数据总数  要么返回0 后续返回数据组的长度
     */
    private Integer resCount(Map<String, Object> map) {
        Integer count = 0;
        if (!map.containsKey("storeid")) {
            if (!map.containsKey("select")) {
                count = storeService.findCount();
            }
        }
        return count;
    }
}
